home *** CD-ROM | disk | FTP | other *** search
- Path: holland.tessi.com!not-for-mail
- From: paulb@holland.tessi.com (Paul Blattner)
- Newsgroups: comp.lang.c++
- Subject: Re: friend functions and access to private variables...
- Date: 8 Mar 1996 18:00:17 -0800
- Organization: Quality Software Engineering, Inc.
- Message-ID: <4hqonh$dn7@holland.tessi.com>
- References: <Dnyy92.MLL@news.uwindsor.ca>
- NNTP-Posting-Host: holland.tessi.com
- Keywords: friend
-
- In article <Dnyy92.MLL@news.uwindsor.ca>,
- Saed Aryan <saed@engn.uwindsor.ca> wrote:
- >Hi all,
- >
- > A friend method of a non friend class has no access to the private member data.
- > Apparently the class to which that method belongs must be a friend. The friend
- > declaration of that method is then redundant.
-
- Actually, your conclusion is *NOT* accurate. THe problem is that the
-
- friend void yMethod(X x);
-
- refers to a *global* function rather than a member of Y. What you needed is
- added below.
-
- >
- >The code snippet below (compilable) will show what works and what doesn't.
- >
- >Aryan.
- >
- >//---- begin code
- >class X {
- > friend void yMethod(X x); // this is for a global (not member) function
-
- friend void Y::yMethod(X x); // adding this makes the call below work
-
- > friend void aFunction(X x);
- > friend class Z;
- > private: int a;
- > };
- >
-
- . . .
-
-
- >class Y { public: void yMethod(X x){x.a=1;}; };
- >// OK now: member `a' is a private member of class `X'
- >// this is Dariusz's question
- >
-
- I hope that you find this helpful,
-
- Paul Blattner
- Quality Software Engineering, Inc.
- (503) 538-8256
- --
- Quality Software Engineering, Inc. The Successful Transition Seminar
- PO Box 303 "Helping companies make a successful
- Beaverton OR 97075 transition to object technology"
- (503) 538-8256 Training in C++ and Tools.h++
-